package com.shouhuobao.bhi.stamp;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.StampHistoryBean;

/**
 * 邮票购买记录
 */
public class StampBuyHistoryActivity extends BaseActivity2 {
	private ListView mListView;
	private StampHistoryAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		showLoadingDialog(null);
		AppHttp.getInstance().stampBuyHistoty();
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.stamp_history_list);
		super.findViewById();
		getAppTitle().setCommonTitle("购买记录");
		mListView = (ListView) findViewById(R.id.listview);
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				showLoadingDialog("");
				AppHttp.getInstance().stampBuyHistoty();
			}
		});

		adapter = new StampHistoryAdapter(null, this);
		mListView.setAdapter(adapter);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.stamp_history:
			cancelLoadingDialog();
			onRefreshComplete();
			final AppResult<StampHistoryBean> result = JSON.parseList(msg.obj, StampHistoryBean.class);
			if (result.isSuccess()) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						ArrayList<StampHistoryBean> resultList = result.getResultList();
						adapter.setItems(resultList, true);
					}
				});

			} else {
				showToast(result.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}
}
